package org.jeecg.modules.demo.soSettlement.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.dto.message.TemplateMessageDTO;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.cwSaleInvoice.entity.CwSaleInvoice;
import org.jeecg.modules.demo.cwSaleInvoice.mapper.CwSaleInvoiceMapper;
import org.jeecg.modules.demo.pubuyorderToRk.mapper.PuBuyOrderToRkMapper;
import org.jeecg.modules.demo.pusettlement.entity.PuSettlement;
import org.jeecg.modules.demo.pusettlement.mapper.PuSettlementMapper;
import org.jeecg.modules.demo.soOrderDetail.mapper.SoOrderDetailMapper;
import org.jeecg.modules.demo.soQualityOrder.entity.SoQualityOrder;
import org.jeecg.modules.demo.soQualityOrder.mapper.SoQualityOrderMapper;
import org.jeecg.modules.demo.soSaleOrder.entity.SoSaleOrder;
import org.jeecg.modules.demo.soSaleOrder.mapper.SoSaleOrderMapper;
import org.jeecg.modules.demo.soSettlement.entity.SoSettlement;
import org.jeecg.modules.demo.soSettlement.entity.SoSettlementVo;
import org.jeecg.modules.demo.soSettlement.mapper.SoSettlementMapper;
import org.jeecg.modules.demo.soSettlement.service.ISoSettlementService;
import org.jeecg.modules.demo.util.generateOddUtilXs;
import org.jeecg.modules.demo.utils.generateOddUtil;
import org.jeecg.modules.demo.warehouseorder.entity.WarehouseOrder;
import org.jeecg.modules.demo.warehouseorder.mapper.WarehouseOrderMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description: so_settlement
 * @Author: jeecg-boot
 * @Date:   2021-12-15
 * @Version: V1.0
 */
@Service
public class SoSettlementServiceImpl extends ServiceImpl<SoSettlementMapper, SoSettlement> implements ISoSettlementService {

    @Resource
    private SoSettlementMapper soSettlementMapper;
    @Resource
    private SoQualityOrderMapper soQualityOrderMapper;
    @Resource
    private SoSaleOrderMapper soSaleOrderMapper;
    @Resource
    private ISysBaseAPI sysBaseAPI;
    @Resource
    private CwSaleInvoiceMapper cwSaleInvoiceMapper;
    @Resource
    private SoOrderDetailMapper soOrderDetailMapper;
    @Resource
    private PuBuyOrderToRkMapper puBuyOrderToRkMapper;
    @Resource
    private WarehouseOrderMapper warehouseOrderMapper;
    @Resource
    private PuSettlementMapper puSettlementMapper;

    private String getMessage(String name,String date) {
        String str ="由【"+name+"】于【"+date+"】发起的销售结算单需要您的审核，请及时查看！";
        return str;
    }
    private static final String TITLE = "单据审核";


	@Override
    public List<SoSettlement> getWriteOffDetails(String str){
        return soSettlementMapper.getWriteOffDetails( str);
    }

    @Override
    public List<SoSettlement> getHxnoBySosNo(String sosNo) {
        return soSettlementMapper.getHxnoBySosNo(sosNo);
    }


    @Override
    public String getMaxCode(){
        return soSettlementMapper.getMaxCode();
    }
    @Override
    public SoSettlement getBySosNo(String sosNo) {
        return soSettlementMapper.getBySosNo(sosNo);
    }

    @Override
    public SoSettlement getSoSettleBySosNo(String sosNo) {
        return soSettlementMapper.getSoSettleBySosNo(sosNo);
    }

    @Override
    public List<SoSettlement> getBysoNo(String id) {
        return soSettlementMapper.getBysoNo(id);
    }

    @Override
    public List<SoSettlementVo> queryXsqsBymonth() {
        return soSettlementMapper.queryXsqsBymonth();
    }

    @Override
    public List<SoSettlementVo> querySrfx(String beginTime,String endTime) {
        return soSettlementMapper.querySrfx(beginTime,endTime);
    }

    @Override
    public List<SoSettlement> getByfpNo(String invoiceNo) {
        return soSettlementMapper.getByfpNo(invoiceNo);
    }

    @Override
    public List<SoSettlement> getYjsd(String soNo) {
        return soSettlementMapper.getYjsd(soNo);
    }

    @Override
    public SoSettlement getBySqoNo(String qoNum) {
        return soSettlementMapper.getBySqoNo(qoNum);
    }

    @Override
    @Transactional
    public boolean hcYjsd(JSONObject jsonObject, HttpServletRequest req) {
        try {
            SoSettlement soset = JSON.parseObject(jsonObject.toJSONString(), SoSettlement.class);
            SoQualityOrder sqo = JSON.parseObject(jsonObject.toJSONString(), SoQualityOrder.class);
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            SoQualityOrder soQualityOrder = soQualityOrderMapper.editsqoStatus(sqo.getQoNum());

            List<SoSettlement> list = soSettlementMapper.getList();
            Collections.sort(list);
            String max_code = "";
            if (list.size() > 0) {
                max_code = list.get(0).getSosNo();
            }
            Map<String,String> codeMap = new HashMap<>();
            if(max_code == null){
                codeMap.put("maxCode",null);
            }else{
                codeMap.put("maxCode",max_code);
            }
            String tenantId = req.getHeader("tenant-id");
            String tname = soSaleOrderMapper.getTname(tenantId);
            String xsjs = generateOddUtilXs.createCode(codeMap,tname+"JS_");

            SoSaleOrder ss = soSaleOrderMapper.getBysoNo(sqo.getSoOrder());
            if (sqo.getSyNum().compareTo(soset.getKjsNum()) > -1) { //剩余质检数量大于等于预结算单剩余可结算数量

                SoSettlement soSettlement = new SoSettlement();
                soSettlement.setId("");
                soSettlement.setSoqNo(sqo.getQoNum());
                soSettlement.setSosNo(xsjs);
                soSettlement.setSosTotal(soset.getKjsNum());
                soSettlement.setSosCdate(new Date());
                soSettlement.setSosStaff(sysUser.getRealname());
                soSettlement.setIsDelete(0);
                soSettlement.setIsFpd(1);
//                soSettlement.setSoStatus(0);
                soSettlement.setSoNo(sqo.getSoOrder());
                soSettlement.setPrdName(sqo.getPrdNo());
                soSettlement.setDept(sqo.getDept());
                soSettlement.setSoCustom(sqo.getSoCustom());
                soSettlement.setProject(sqo.getProject());
                soSettlement.setCreateTime(new Date());
                soSettlement.setSosPrice(soset.getSosPrice());
                soSettlement.setSosAmount(soset.getKjsNum().multiply(soset.getSosPrice()));
                soSettlement.setJsType(3);
                soSettlement.setSoStatus(1);
                soSettlement.setShName(sqo.getShName());
                soSettlement.setHxNo(soset.getSosNo());
                soSettlement.setTradecode(ss.getTradecode());
                soSettlementMapper.insert(soSettlement);
                soQualityOrder.setChuku(1);
                soQualityOrder.setSyNum(soQualityOrder.getSyNum().subtract(soset.getKjsNum()));
//            soset.setSoqNo(sqo.getQoNum());
                ss.setKjskpNum(ss.getKjskpNum().subtract(soset.getKjsNum()));
                ss.setKjsJine(ss.getKjsJine().subtract(soset.getKjsJine()));
                if (soQualityOrder.getSyNum().compareTo(BigDecimal.valueOf(0)) == 1){ //>
                    soQualityOrder.setChuku(2);
                }
                soQualityOrderMapper.updateById(soQualityOrder);
                soSaleOrderMapper.updateById(ss);
                soset.setKjsNum(BigDecimal.valueOf(0.00));
                soset.setKjsJine(BigDecimal.valueOf(0.00));
                soSettlementMapper.updateById(soset);

            } else if (sqo.getSyNum().compareTo(soset.getKjsNum()) == -1) {
                SoSettlement soSettlement = new SoSettlement();
                soSettlement.setId("");
                soSettlement.setSoqNo(sqo.getQoNum());
                soSettlement.setSosNo(xsjs);
                soSettlement.setSosTotal(sqo.getSyNum());
                soSettlement.setSosCdate(new Date());
                soSettlement.setSosStaff(sysUser.getRealname());
                soSettlement.setIsDelete(0);
                soSettlement.setIsFpd(1);
//                soSettlement.setSoStatus(0);
                soSettlement.setSoNo(sqo.getSoOrder());
                soSettlement.setPrdName(sqo.getPrdNo());
                soSettlement.setDept(sqo.getDept());
                soSettlement.setSoCustom(sqo.getSoCustom());
                soSettlement.setProject(sqo.getProject());
                soSettlement.setCreateTime(new Date());
                soSettlement.setSosPrice(soset.getSosPrice());
                soSettlement.setSosAmount(sqo.getSyNum().multiply(soset.getSosPrice()));
                soSettlement.setJsType(3);
                soSettlement.setSoStatus(1);
                soSettlement.setShName(sqo.getShName());
                soSettlement.setHxNo(soset.getSosNo());
                soSettlement.setTradecode(ss.getTradecode());
                soSettlementMapper.insert(soSettlement);
                soQualityOrder.setSyNum(soQualityOrder.getSyNum().subtract(sqo.getSyNum()));
                soQualityOrder.setChuku(1);
                soset.setKjsNum(soset.getKjsNum().subtract(sqo.getSyNum()));
                soset.setKjsJine(soset.getKjsJine().subtract(sqo.getSyNum().multiply(soset.getSosPrice())));
                ss.setKjskpNum(ss.getKjskpNum().subtract(sqo.getSyNum()));
                ss.setKjsJine(ss.getKjsJine().subtract(sqo.getSyNum().multiply(soset.getSosPrice())));
                soQualityOrderMapper.updateById(soQualityOrder);
                soSettlementMapper.updateById(soset);
                soSaleOrderMapper.updateById(ss);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    @Transactional
    public boolean addSoSettle(JSONObject jsonObject, HttpServletRequest req) {
        try {
            SoSettlement soSettlement = JSON.parseObject(jsonObject.toJSONString(), SoSettlement.class);
            SoQualityOrder sqo = JSON.parseObject(jsonObject.toJSONString(), SoQualityOrder.class);
            String str = soSettlementMapper.getMaxCode();
            Map<String, String> codeMap = new HashMap<>();
            if (str == null) {
                codeMap.put("maxCode", null);
            } else {
                codeMap.put("maxCode", str);
            }
            String tenantId = req.getHeader("tenant-id");
            String tname = soSaleOrderMapper.getTname(tenantId).toUpperCase();
            String name =tname+ "JS_";
            String resultCode = generateOddUtil.createCode(codeMap,name);
            soSettlement.setId("");
            soSettlement.setSoqNo(sqo.getQoNum());
            soSettlement.setSosNo(resultCode);
//            soSettlement.setSosTotal(sqo.getQoQuantity());
            soSettlement.setSosTotal(sqo.getSyNum());
            if (soSettlement.getSosCdate() == null) {
                soSettlement.setSosCdate(new Date());
            }
            soSettlement.setSosStaff(sqo.getSosStaff());
            soSettlement.setIsDelete(0);
            soSettlement.setIsFpd(0);
            soSettlement.setSoStatus(0);
            soSettlement.setSoNo(sqo.getSoOrder());
            soSettlement.setPrdName(sqo.getPrdNo());
//            soSettlement.setDept(sqo.getDept());
//            soSettlement.setSoCustom(sqo.getSoCustom());
            soSettlement.setSosNum(sqo.getQoQuantity());
//            soSettlement.setSosNum(sqo.getQoQuantity().add(soSettlement.getKouton()));
            soSettlement.setProject(sqo.getProject());
            soSettlement.setCreateTime(new Date());
            soSettlement.setHtjjia(soSettlement.getHsdj());
//            soSettlement.setYkpJine(BigDecimal.valueOf(0.00));
            soSettlement.setWkpJine(soSettlement.getHsje());
//			 soSettlement.setSosPrice(soSettlement.getHtjjia().add(soSettlement.getAdjust()).add(soSettlement.getShouyi()));
//            soSettlement.setSosAmount(soSettlement.getSosPrice().multiply(soSettlement.getSosTotal()));
            soSettlement.setSosAmount(soSettlement.getHsje());
            soSettlement.setJsType(1);
            if (sqo.getQoQuantity().compareTo(sqo.getSyNum()) == 1){  //质检单质检数量>剩余质检数量（说明冲销过预结算单、红冲等，需要生成蓝字回冲单4）
                soSettlement.setJsType(4);
            }
//            soSettlement.setTradecode();
            soSettlement.setPsTax(soSettlement.getHsje().subtract(soSettlement.getWsje()));
//            soSettlement.setKjsJine(csi.getSkJine());
//            soSettlement.setKjsNum(csi.getFpsl());
            if (soSettlement.getFj() == null || soSettlement.getFj().equals("")) {
                soSettlement.setHqzy(0);
            } else {
                soSettlement.setHqzy(1);
            }
            soSettlementMapper.insert(soSettlement);
            String qoNum = sqo.getQoNum();
            String[] soq = qoNum.split(",");
            for (String sq : soq) {
                SoQualityOrder so = soQualityOrderMapper.editsqoStatus(sq);
                so.setChuku(1);
                soQualityOrderMapper.updateById(so);
            }



            LoginUser user = sysBaseAPI.getUserById(soSettlement.getSosStaff());
            TemplateMessageDTO templateMessageDTO = new TemplateMessageDTO();
            templateMessageDTO.setFromUser("管理员");
            templateMessageDTO.setToUser(soSettlement.getShName()+",");
            templateMessageDTO.setTitle(TITLE);
            templateMessageDTO.setTemplateCode("order");
            Map<String, String> templateParam = new HashMap<>();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date= simpleDateFormat.format(new Date());
            templateParam.put("ts_date",date);
            templateParam.put("ts_content", getMessage(user != null?user.getRealname():"该业务员不存在！", date));
            templateParam.put("checkOrder",soSettlement.getSosNo());
            templateParam.put("cd_url","/sosettlement/SoSettlementList/list");
            templateMessageDTO.setTemplateParam(templateParam);
            sysBaseAPI.sendTemplateAnnouncement(templateMessageDTO);

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    //入库单生成结算单方法
    @Override
    @Transactional
    public boolean addPoSettle(JSONObject jsonObject, HttpServletRequest req) {
        try{
            PuSettlement soSettlement = JSON.parseObject(jsonObject.toJSONString(),PuSettlement.class);
            WarehouseOrder puBuyOrderToRk = JSON.parseObject(jsonObject.toJSONString(), WarehouseOrder.class);
            String str = puSettlementMapper.getMaxCode();
            Map<String,String> codeMap = new HashMap<>();
            if(str == null){
                codeMap.put("maxCode",null);
            }else{
                codeMap.put("maxCode",str);
            }
            String tenantId = req.getHeader("tenant-id");
            String tname = soSaleOrderMapper.getTname(tenantId).toUpperCase();
            String name = tname + "JS_";
            String resultCode = generateOddUtil.createCode(codeMap,name);
            soSettlement.setId("");
            soSettlement.setWoNo(puBuyOrderToRk.getPoOrder());
            soSettlement.setPusNo(resultCode);
            soSettlement.setQoQuantity(puBuyOrderToRk.getQoQuantity());
            soSettlement.setPoTotail(puBuyOrderToRk.getQoQuantity());
            if(soSettlement.getPusCdate() == null){
                soSettlement.setPusCdate(new Date());
            }
            if(jsonObject.get("sosStaff") != null){
                soSettlement.setPoStaff(jsonObject.get("sosStaff").toString());
            }

            if(jsonObject.get("dept") != null){
                soSettlement.setDepartment(jsonObject.get("dept").toString());
            }

            if(jsonObject.get("cinvname") != null){
                soSettlement.setCinvname(jsonObject.get("cinvname").toString());
            }

            if(jsonObject.get("zjzyf") != null){
                soSettlement.setZljf(BigDecimal.valueOf(Double.parseDouble(jsonObject.get("zjzyf").toString())));
            }


            soSettlement.setPsTaxrat(13);


            if(jsonObject.get("txfy") != null){
                soSettlement.setTiexian(BigDecimal.valueOf(Double.parseDouble(jsonObject.get("txfy").toString())));
            }

            //soSettlement.setSosStaff(puBuyOrderToRk.getPoStaff());
            soSettlement.setShanchu(0);
            soSettlement.setIsInvoice(0);
            soSettlement.setShenhe(0);
            //soSettlement.set(puBuyOrderToRk.getPoOrder());

            soSettlement.setPayTotail(puBuyOrderToRk.getQoQuantity());
            soSettlement.setProject(puBuyOrderToRk.getProject());
            soSettlement.setCreateTime(new Date());
            soSettlement.setHtjjia(soSettlement.getHsdj());
            soSettlement.setHsje(soSettlement.getHsje());
            soSettlement.setHsdj(puBuyOrderToRk.getHsdj());
            soSettlement.setIsInvoice(0);
            soSettlement.setDjlx(3);
            soSettlement.setPsTax(soSettlement.getHsje().subtract(soSettlement.getWsje()));
            soSettlement.setFpsl(puBuyOrderToRk.getQoQuantity());
            soSettlement.setRealityTotail(puBuyOrderToRk.getQoQuantity());
            soSettlement.setKtotail(puBuyOrderToRk.getQoQuantity());
            soSettlement.setDtotail(puBuyOrderToRk.getQoQuantity());
            soSettlement.setMeasureunit("吨");


//            if (soSettlement.getFj() == null || soSettlement.getFj().equals("")) {
//                soSettlement.setHqzy(0);
//            } else {
//                soSettlement.setHqzy(1);
//            }
            String qoNum = "";
            if(jsonObject.get("woNo") != null){
                qoNum = jsonObject.get("woNo").toString();
                soSettlement.setWoNo(jsonObject.get("woNo").toString());
            }


            if(jsonObject.get("tradecode") != null){
                soSettlement.setTradecode(jsonObject.get("tradecode").toString());
            }

            if(jsonObject.get("shName") != null){
                soSettlement.setSaleman(jsonObject.get("shName").toString());
            }



            puSettlementMapper.insert(soSettlement);
            String[] qoNs = qoNum.split(",");
            for(String qon : qoNs){
                WarehouseOrder pu = warehouseOrderMapper.getByWoNo(qon);
                pu.setIsSettlement(2);
                warehouseOrderMapper.updateById(pu);
            }

            LoginUser user = sysBaseAPI.getUserById(soSettlement.getPoStaff());
            TemplateMessageDTO templateMessageDTO = new TemplateMessageDTO();
            templateMessageDTO.setFromUser("管理员");
            templateMessageDTO.setToUser(soSettlement.getSaleman()+",");
            templateMessageDTO.setTitle(TITLE);
            templateMessageDTO.setTemplateCode("order");
            Map<String, String> templateParam = new HashMap<>();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date= simpleDateFormat.format(new Date());
            templateParam.put("ts_date",date);
            templateParam.put("ts_content", getMessage(user != null?user.getRealname():"该业务员不存在！", date));
            templateParam.put("checkOrder",soSettlement.getPoOrder());
            templateParam.put("cd_url","//warehouseorder/warehouseOrder/list");
            templateMessageDTO.setTemplateParam(templateParam);
            sysBaseAPI.sendTemplateAnnouncement(templateMessageDTO);

            return true;


        }catch(Exception e){
            e.printStackTrace();
            //异常回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }

    }

    @Override
    @Transactional
    public boolean addSoInvoice(JSONObject jsonObject, HttpServletRequest req) {
        try {
            SoSettlement soSettlement = JSON.parseObject(jsonObject.toJSONString(), SoSettlement.class);
            CwSaleInvoice csi = JSON.parseObject(jsonObject.toJSONString(), CwSaleInvoice.class);
            String max_code = cwSaleInvoiceMapper.getMaxCode();
            Map<String, String> codeMap = new HashMap<>();
            if (max_code == null) {
                codeMap.put("maxCode", null);
            } else {
                codeMap.put("maxCode", max_code);
            }
            String tenantId = req.getHeader("tenant-id");
            String tname = soSaleOrderMapper.getTname(tenantId);
            String xsfp = generateOddUtilXs.createCode(codeMap, tname + "FP_");

            SoSettlement ssm = soSettlementMapper.getSoSettleBySosNo(soSettlement.getSosNo());
//            if (csi.getInvoiceNo() == null && csi.getInvoiceNo() == "" && csi.getInvoiceNo().isEmpty()) {
            csi.setId("");
//            csi.setPoStaff(soSettlement.getSosStaff());
            csi.setProject(soSettlement.getProject());
//            csi.setDept(ssm.getDept());
            csi.setSoCustom(ssm.getSoCustom());
            csi.setInvoiceNo(xsfp);
            csi.setSosNo(ssm.getSosNo());
            csi.setSoNo(ssm.getSoNo());
            csi.setIsDelete(0);
            csi.setIsSkd(0);
            csi.setSoStatus(0);
            csi.setFplx("2");
            csi.setSyysJine(BigDecimal.valueOf(0));
            csi.setSkJine(csi.getSkJines());
            csi.setSupamount(csi.getSkJine());
            csi.setPrdName(soSettlement.getPrdName());
            csi.setRealzb(soSettlement.getRealzb());
            csi.setHsdj(soSettlement.getHsdj());
            csi.setHsje(soSettlement.getHsdj().multiply(soSettlement.getFpsl()));
            csi.setWsdj(soSettlement.getHsdj().divide(BigDecimal.valueOf(1.13),7,BigDecimal.ROUND_HALF_UP));
            csi.setWsje(csi.getWsdj().multiply(soSettlement.getFpsl()));
            csi.setPsTax(csi.getHsje().subtract(csi.getWsje()));
            if (soSettlement.getSosCdate() == null) {
                csi.setScCdate(new Date());
            } else {
                csi.setScCdate(soSettlement.getSosCdate());
            }

//            List<SoOrderDetail> sod = soOrderDetailMapper.getBysoNo(csi.getSoNo());
//            for (SoOrderDetail sd : sod){
//                BigDecimal cbdj = sd.getCbJine().divide(sd.getXclNum(),7,BigDecimal.ROUND_HALF_UP);
//                List<SoQualityOrder> ssq = soQualityOrderMapper.selectBysodNo(sd.getSodNo());
//                for (SoQualityOrder sq : ssq){
////                    List<SoQualityOrder> ssq = soQualityOrderMapper.selectBysodNo(sd.getSodNo());
//
//                    SoSettlement sos = soSettlementMapper.getBySosNo(sq.getQoNum());
//                }
//            }
//
//            String[] soq = sos.getSoqNo().split(",");
////            for (String sq : soq){
////                SoQualityOrder soQualityOrder = soQualityOrderMapper.editsqoStatus(sq);
////            }
//            List<CwSaleInvoice> cs = cwSaleInvoiceMapper.getlistBySosNo(csi.getSosNo());

//            csi.setCreateTime(new Date());
            cwSaleInvoiceMapper.insert(csi);

            ssm.setWkpJine(ssm.getWkpJine().subtract(csi.getSkJine()));
//                ssm.setIsFpd(1);
            if (ssm.getWkpJine().compareTo(BigDecimal.valueOf(0)) < 1) {
                ssm.setIsFpd(1);
            }
            soSettlementMapper.updateById(ssm);

//            }

            LoginUser user = sysBaseAPI.getUserById(csi.getPoStaff());
            TemplateMessageDTO templateMessageDTO = new TemplateMessageDTO();
            templateMessageDTO.setFromUser("管理员");
            templateMessageDTO.setToUser(csi.getShName() + ",");
            templateMessageDTO.setTitle(TITLE);
            templateMessageDTO.setTemplateCode("order");
            Map<String, String> templateParam = new HashMap<>();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date = simpleDateFormat.format(new Date());
            templateParam.put("ts_date", date);
            templateParam.put("ts_content", getMessage(user != null?user.getRealname():"该业务员不存在！", date));
            templateParam.put("checkOrder", csi.getInvoiceNo());
            templateParam.put("cd_url", "/collection/CwSaleInvoiceList");
            templateMessageDTO.setTemplateParam(templateParam);
            sysBaseAPI.sendTemplateAnnouncement(templateMessageDTO);


            return true;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

}
